package com.kingsalert.kingsalert.activity;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.kingsalert.kingsalert.R;
import com.kingsalert.kingsalert.base.BaseActivity;
import com.kingsalert.kingsalert.dialog.CustomerServiceDialog;
import com.kingsalert.kingsalert.util.http.HttpUtil;

public class FeedbackActivity extends BaseActivity {

    private String type = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);

        page = "FEEDBACK";

        findViewById(R.id.afbk_ib_back).setOnClickListener(v -> onBackPressed());
        findViewById(R.id.afbk_ib_cs).setOnClickListener(v -> new CustomerServiceDialog(this).show());

        TextView[] typeTvs = new TextView[]{
            findViewById(R.id.afbk_tv_productsuggestion),
            findViewById(R.id.afbk_tv_appbugs),
            findViewById(R.id.afbk_tv_paymentissues),
            findViewById(R.id.afbk_tv_others)
        };

        for (int i=0;i<typeTvs.length;i++){
            final int fi = i;
            typeTvs[i].setOnClickListener(v -> {
                for (int j=0;j<typeTvs.length;j++){
                    if (fi==j){

                        type = typeTvs[j].getText().toString();
                        HttpUtil.getInstance().saveBuriedPoint(page,type.toUpperCase()+"_CLICK","");

                        typeTvs[j].setBackgroundResource(R.drawable.bg_green_ll_rc24dp_border_green_1dp);
                        typeTvs[j].setTextColor(getColor(R.color.green_main));

                    }else {
                        typeTvs[j].setBackgroundResource(R.drawable.bg_gray_rc24dp);
                        typeTvs[j].setTextColor(Color.parseColor("#FF6E6E6E"));
                    }
                }
            });
        }

        View hintView = findViewById(R.id.afbk_view_hint);
        EditText feedbackEt = findViewById(R.id.afbk_et_feedback);
        TextView countTv = findViewById(R.id.afbk_tv_count);

        feedbackEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            @Override
            public void afterTextChanged(Editable s) {
                if (s.length()>0 && hintView.getVisibility()==View.VISIBLE) hintView.setVisibility(View.GONE);
                else if (s.length()==0 && hintView.getVisibility()==View.GONE) hintView.setVisibility(View.VISIBLE);
            }
        });

        feedbackEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            @SuppressLint("SetTextI18n")
            @Override
            public void afterTextChanged(Editable s) {
                int length = s.length();
                countTv.setText(length+"/1000");
                if (length>=1000) countTv.setTextColor(Color.RED);
                else countTv.setTextColor(Color.parseColor("#FF434343"));
            }
        });

        feedbackEt.setOnFocusChangeListener((v, hasFocus) ->
            HttpUtil.getInstance().saveBuriedPoint(page,"INPUT",hasFocus ? "":feedbackEt.getText().toString().trim())
        );

        findViewById(R.id.afbk_btn_submit).setOnClickListener(v -> {
            String feedback = feedbackEt.getText().toString().trim();
            if (TextUtils.isEmpty(type) || TextUtils.isEmpty(feedback)) {
                Toast.makeText(this,getString(R.string.tips_input_empty),Toast.LENGTH_SHORT).show();
                return;
            }
            HttpUtil.getInstance().submitFeedback(this,type,feedback,this::onSubmitResponse);
        });

    }

    public void onSubmitResponse(){

        Toast.makeText(this,R.string.tips_submit_success,Toast.LENGTH_SHORT).show();

        new Thread(() -> {
            try {
                Thread.sleep(1001);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            runOnUiThread(this::onBackPressed);
        }).start();
    }

}